package com.hmkj.web.controller.pay;

import com.hmkj.core.api.hrpay.HRPayUtils;
import com.hmkj.core.api.hrpay.utils.ConfigUtils;
import com.hmkj.core.api.hrpay.utils.SSLClient;
import com.hmkj.core.api.hrpay.utils.SignUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.amount.Recharge;
import com.hmkj.core.service.amount.CashService;
import com.hmkj.core.service.amount.RechargeService;
import com.hmkj.web.controller.GenericController;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 华融支付异步通知
 *
 * @author Vector
 * @create 2018-01-12 10:18
 */

@Slf4j
@RestController
@RequestMapping(GenericController.BASE_API_URL + "/notify/hr")
public class HRPayNotifyController extends GenericController<Integer, Recharge> {

    @Resource
    private CashService cashService;

    @Resource
    private RechargeService rechargeService;


    /**
     * 快捷支付异步通知
     */
    @ResponseBody
    @RequestMapping(value = "/doNotify", method = RequestMethod.POST)
    public void doNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String> retMap = new LinkedHashMap<>();
        retMap.put("acctNo", request.getParameter("acctNo"));
        retMap.put("merNo", request.getParameter("merNo"));
        retMap.put("orderDate", request.getParameter("orderDate"));
        retMap.put("orderNo", request.getParameter("orderNo"));
        retMap.put("productId", request.getParameter("productId"));
        retMap.put("respCode", request.getParameter("respCode"));
        retMap.put("respDesc", request.getParameter("respDesc"));
        retMap.put("Token", request.getParameter("Token"));
        retMap.put("transAmt", request.getParameter("transAmt"));
        retMap.put("transId", request.getParameter("transId"));
        retMap.put("signature", request.getParameter("signature"));

        if (!HRPayUtils.verferSignData(retMap)) {
            throw new BussinessException("快捷支付异步通知验签失败");
        }
        rechargeService.doRechargeBackHR(retMap);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print("SUCCESS");
        out.flush();
        out.close();
    }

    /**
     * 网银B2C支付异步通知
     */
    @ResponseBody
    @RequestMapping(value = "/doB2CNotify", method = RequestMethod.POST)
    public void doB2CNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String> retMap = new LinkedHashMap<>();
//        retMap.put("respCode", request.getParameter("respCode"));
//        retMap.put("orderNo", request.getParameter("orderNo"));
//        retMap.put("transId", request.getParameter("transId"));
//        retMap.put("orderDate", request.getParameter("orderDate"));
//        retMap.put("respDesc", request.getParameter("respDesc"));
//        retMap.put("transAmt", request.getParameter("transAmt"));
//        retMap.put("signature", request.getParameter("signature"));
//        retMap.put("merNo", request.getParameter("merNo"));
//        retMap.put("productId", request.getParameter("productId"));
        retMap.put("acctNo", request.getParameter("acctNo"));
        retMap.put("merNo", request.getParameter("merNo"));
        retMap.put("orderDate", request.getParameter("orderDate"));
        retMap.put("orderNo", request.getParameter("orderNo"));
        retMap.put("productId", request.getParameter("productId"));
        retMap.put("respCode", request.getParameter("respCode"));
        retMap.put("respDesc", request.getParameter("respDesc"));
        retMap.put("Token", request.getParameter("Token"));
        retMap.put("transAmt", request.getParameter("transAmt"));
        retMap.put("transId", request.getParameter("transId"));
        retMap.put("signature", request.getParameter("signature"));
        if (!HRPayUtils.verferSignData(retMap)) {
            throw new BussinessException("网银B2C异步通知验签失败");
        }
        rechargeService.doRechargeBackHRB2C(retMap);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print("SUCCESS");
        out.flush();
        out.close();
    }


    /**
     * 代付异步通知
     */
    @ResponseBody
    @RequestMapping(value = "/dfNotify", method = RequestMethod.POST)
    public void dfNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String> retMap = new LinkedHashMap<>();
        retMap.put("acctNo", request.getParameter("acctNo"));
        retMap.put("merNo", request.getParameter("merNo"));
        retMap.put("orderDate", request.getParameter("orderDate"));
        retMap.put("orderNo", request.getParameter("orderNo"));
        retMap.put("productId", request.getParameter("productId"));
        retMap.put("respCode", request.getParameter("respCode"));
        retMap.put("respDesc", request.getParameter("respDesc"));
        retMap.put("Token", request.getParameter("Token"));
        retMap.put("transAmt", request.getParameter("transAmt"));
        retMap.put("transId", request.getParameter("transId"));
        retMap.put("signature", request.getParameter("signature"));

        if (!HRPayUtils.verferSignData(retMap)) {
            throw new BussinessException("代付支付异步通知验签失败");
        }
        cashService.doCashNotifyHR(retMap);

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print("SUCCESS");
        out.flush();
        out.close();
    }

    /**
     * 交易状态查询
     */
//    @Resource
//    @RequestMapping(value = "queryTransStatus",method = RequestMethod.POST)
//     public void queryTransStatus () throws Exception {
//        DefaultHttpClient httpClient = new SSLClient();
//        HttpPost postMethod = new HttpPost(ConfigUtils.getProperty("trans_url"));
//        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
//        nvps.add(new BasicNameValuePair("requestNo", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())));
//        nvps.add(new BasicNameValuePair("version", "V1.0"));
//        nvps.add(new BasicNameValuePair("transId", "04"));
//        nvps.add(new BasicNameValuePair("merNo", ConfigUtils.getProperty("merchant_no")));
//        nvps.add(new BasicNameValuePair("orderDate", "20161229"));
//        nvps.add(new BasicNameValuePair("orderNo", "20161229141100"));
//        nvps.add(new BasicNameValuePair("signature", SignUtils.signData(nvps)));
//        try {
//            postMethod.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
//            HttpResponse resp = httpClient.execute(postMethod);
//            String str = EntityUtils.toString(resp.getEntity(), "UTF-8");
//            int statusCode = resp.getStatusLine().getStatusCode();
//            if (200 == statusCode) {
//                boolean signFlag = SignUtils.verferSignData(str);
//                if (!signFlag) {
//                    System.out.println("验签失败");
//                    return;
//                }
//                System.out.println("验签成功");
//                return;
//            }
//            System.out.println("返回错误码:" + statusCode);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

}
